package com.study.services;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import com.study.model.jdao.ICollectionDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.IFormDAO;
import com.study.model.jdao.IFormDetailsDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.ColleDAO;
import com.study.model.mdao.FormDetailsDAO;
import com.study.model.mdao.FormMapper;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Dept;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Form;
import com.study.model.pojo.FormsDetail;
import com.study.model.pojo.User;

/**
 * 集客信息查询
 * 
 * @author 郭星
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FormService {

	@Autowired
	FormMapper dao;

	@Autowired
	FormDetailsDAO formdetailsdao;

	@Autowired
	ColleDAO colledao;

	@Autowired
	IFormDAO jformDao;

	@Autowired
	IFormDetailsDAO jformdetaildao;

	@Autowired
	ICollectionDAO jcolledao;

	@Autowired
	IUserDAO iuserdao;

	@Autowired
	IDictionaryDAO idictionarydao;

	/**
	 * 查询所有集客 form表信息
	 * 
	 * @return
	 */
	public Iterable<Form> selectAllForm() {
		/*
		 * Iterable<Form> findAll = jformDao.findAll(); for (Form form : findAll) {
		 * List<FormsDetail> formsDetails = form.getFormsDetails(); for (FormsDetail
		 * form2 : formsDetails) { System.out.println(form2); } }
		 */
		return jformDao.findAll();
		// return dao.selectAll();
	}

	/**
	 * 查询对应表单详情id的集客线索
	 * 
	 * @return
	 */
	public FormsDetail selectForidForm(int ids) {
		System.out.println("---查询对应集客html5中的表单详情信息");
		return jformdetaildao.findByFordetailsId(ids);
		// return formdetailsdao.selectAllById(ids);
	}

	/**
	 * 查询对应集客html5中的表单详情信息
	 * 
	 * @param ids
	 * @return
	 */
	public List<Colle> selectFordetailsIdColle(int ids) {

		return jcolledao.findBycolId(ids);
	}

	/**
	 * 查询当前未处理的五条集客记录
	 * 
	 * @return
	 */
	public List<Colle> selectForDictionry() {
		List<Colle> ends = new ArrayList<>();
		List<Colle> selectColleByDo = colledao.selectColleByDo(1711);
		for (int i = 0; i < selectColleByDo.size(); i++) {
			System.out.println(selectColleByDo.get(i));
			if (i < 5) {
				ends.add(selectColleByDo.get(i));
			}
		}
		return ends;
	}

	/**
	 * 查询对应表单id的表单详情信息,并增加浏览量
	 * 
	 * @param forid
	 * @return
	 */
	public List<FormsDetail> selectByForid(Integer forid) {
		// return formdetailsdao.selectAllById(forid);
		Form findByForId = jformDao.findByForId(forid);
		// findByForId.setForBrowse(findByForId.getForBrowse() + 1);
		return jformdetaildao.findByForids(forid);

	}

	/**
	 * 添加一个colle记录
	 * 
	 * @param photo       电话
	 * @param name        名称
	 * @param needs       需求
	 * @param males       称呼
	 * @param forditailid 表单详情id
	 * @param uid         员工id
	 */
	public void addColle(String photo, String name, String needs, String males, int forditailid, Integer uid) {
		Colle colle = new Colle();
		colle.setColDate(new Date());
		colle.setColLinkman(name);
		colle.setColPhone(photo);
		colle.setColContent(needs);
		colle.setColRemark(males);
		// 设置管理员工
		if (uid != null && uid > 0) {
			User findByuseId = iuserdao.findByUseId(uid);
			colle.setUser(findByuseId);
		}

		// 查询表单详情id的信息
		FormsDetail formsdetails = jformdetaildao.findByFordetailsId(forditailid);
		colle.setFormsDetail(formsdetails);

		// 未处理
		Dictionary status = idictionarydao.findByDicId(1711);
		// 集客
		Dictionary classe = idictionarydao.findByDicId(1722);

		colle.setDictionary1(classe);
		colle.setDictionary2(status);
		jcolledao.save(colle);

	}

	/**
	 * 修改表单表浏览量
	 * 
	 * @param forditailid
	 */
	public void updateFormRorrow(int forid) {

		// 找到当前表单详情id的表单详情id
		Form formsdetails = jformDao.findByForId(forid);
		formsdetails.setForBrowse(formsdetails.getForBrowse() + 1);
	}

	/**
	 * 修改表单详情表浏览量
	 * 
	 * @param forditailid
	 */
	public void updateFormDetailsRorrow(int forditailid) {

		// 找到当前表单详情id的表单详情id
		FormsDetail formsdetails = jformdetaildao.findByFordetailsId(forditailid);
		formsdetails.setFordetailsBrowse(formsdetails.getFordetailsBrowse() + 1);
		updateFormRorrow(formsdetails.getForm().getForId());
	}

	/**
	 * 修改表单详情表信息
	 * 
	 * @param forditailid
	 * @param uid
	 */
	public void updateFormDetails(int forditailid, Integer uid) {
		// TODO Auto-generated method stub
		// 修改表单详情信息
		// 找到当前表单详情id的表单详情id
		FormsDetail formsdetails = jformdetaildao.findByFordetailsId(forditailid);
		List<Colle> colles = formsdetails.getColles();

		// 未处理、总记录数、处理数
		int a = 0, b = 0, c = 0;
		for (Colle colle : colles) {
			System.out.println(colle);
			if (colle.getDictionary2().getDicId() == 1711) {
				a++;
			} else if (colle.getDictionary2().getDicId() == 1712) {
				c++;
			}
			b++;
		}
		System.out.println("未处理:" + a + "总记录数:" + b + "处理数:" + c);
		// 算出转化率
		double result = 0;

		if (b != 0) {
			result = Double.valueOf(new DecimalFormat("#.00").format((double) c / b * 100));
		}
		System.out.println("转化率：" + result);
		formsdetails.setFordetailsChange((int) result);
		formsdetails.setForGuset(c);
	}

	/**
	 * 修改表单表的信息
	 * 
	 * @param forid
	 */
	public void updateForm(Integer forid) {
		List<FormsDetail> selectByForid = selectByForid(forid);
		List<Colle> collelist = new ArrayList<>();

		// 未处理、总记录数、处理数
		int a = 0, b = 0, c = 0;
		for (FormsDetail fd : selectByForid) {
			List<Colle> colles = fd.getColles();
			//System.out.println("此主题拥有" + fd.getColles().get(0));
			for (Colle colle : colles) {
				System.out.println(colle);
				if (colle.getDictionary2().getDicId() == 1711) {
					a++;
				} else if (colle.getDictionary2().getDicId() == 1712) {
					c++;
				}
				b++;
			}
			for (int i = colles.size() - 1; i >= 0; i--) {
				collelist.add(colles.get(i));
			}
		}

		System.out.println("总未处理:" + a + "总记录数:" + b + "总处理数:" + c);
		// 算出转化率
		double result = 0;

		if (b != 0) {
			result = Double.valueOf(new DecimalFormat("#.00").format((double) c / b * 100));
		}
		// 查询表单表对象
		Form findByForId = jformDao.findByForId(forid);
		findByForId.setForChange((int) result);
		findByForId.setForGuest(c);
	}

	/**
	 * 添加一个Html5
	 * 
	 * @param texts
	 * @param title
	 */
	public void addHtml(String texts, String title) {
		String a=null,b=null;
		a=texts;
		b=title;
		a=a.replaceAll("\"", "\'"); 
		System.out.println("现在的字符串:"+a);
		formdetailsdao.addHtml(a,b);
	}

	/**
	 * 查询对应表单详情的信息
	 * @param forid
	 * @return 
	 */
	public JSONObject findhtml(Integer forid) {
		return formdetailsdao.findhtml(forid);
	}
}
